package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    @AutoFill(OperationType.INSERT)
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into dish (name,category_id, price,image,description,status,create_time,update_time,create_user,update_user)" +
            "values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    void insert(Dish dish);

    Page<DishVO> selectByPage(DishPageQueryDTO dto);

    /**
     * 查询ids对应的菜品中，启售中状态的菜品数量
     */
    int selectEnableDishCountByIds(List<Long> ids);

    /**
     * 根据ids集合，批量删除对应的菜品
     */
    void batchDeleteByIds(List<Long> ids);


    @Select("select * from dish where id = #{id}")
    Dish selectById(Long id);

    @AutoFill(OperationType.UPDATE)
    void updateById(Dish dish);

    @Select("select * from dish where category_id = #{categoryId}")
    List<Dish> selectDishesByCategoryId(Long categoryId);

    /**
     * 根据分类id查询菜品列表，及每个菜品关联的分类名称
     * @param categoryId
     * @return
     */
    @Select("select d.*, c.name categoryName from dish d left join category c on d.category_id = c.id where d.category_id = #{categoryId} and d.status = 1")
    List<DishVO> selectEnableListByCategoryId(Long categoryId);

    @Select("select count(*) from dish where status = #{status}")
    Integer countByStatus(Integer status);
}
